class Animal:
    def __init__(self, idx, H, F):
        self.idx = idx  # 动物编号
        self.H = H  # 欢乐值
        self.F = F  # 感动值
        self.right = None  # 右手的动物
        self.left = None  # 左手的动物


def calculate_energy(animals):
    energy = 0
    visited = set()  # 记录已经计算过的圈
    for animal in animals:
        if animal in visited:
            continue
        # 找到当前圈的所有动物
        circle = []
        current = animal
        while True:
            circle.append(current)
            visited.add(current)
            current = current.right
            if current == animal:
                break
        t = len(circle)  # 圈的大小
        # 计算圈中每对动物的欢乐能量
        for i in range(t):
            for j in range(1, t):
                p = j
                energy += (t - p) * circle[(i + j) % t].H * circle[i].F
    return energy


def main(inp1, inp2):
    n = len(inp1)
    animals = [Animal(i + 1, inp1[i][0], inp1[i][1]) for i in range(n)]

    # 初始化圈
    for i in range(n):
        animals[i].right = animals[(i + 1) % n]
        animals[i].left = animals[(i - 1) % n]

    results = []
    for operation in inp2:
        k, p, q = operation
        if k == 1:
            # 变换队形：断开 p 的右手和 q 的左手，重新连接
            p_animal = animals[p - 1]
            q_animal = animals[q - 1]
            # 断开 p 的右手和 q 的左手
            p_right = p_animal.right
            q_left = q_animal.left
            # 重新连接
            p_animal.right = q_animal
            q_animal.left = p_animal
            p_right.left = q_left
            q_left.right = p_right
        elif k == 2:
            # 更新动物 p 的欢乐值为 q
            animals[p - 1].H = q
        elif k == 3:
            # 更新动物 p 的感动值为 q
            animals[p - 1].F = q

        # 计算当前欢乐能量
        energy = calculate_energy(animals)
        results.append(energy)

    return results


# 输入
inp1 = [(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]
inp2 = [(1, 2, 8), (1, 2, 6), (2, 8, 10), (3, 5, 10), (1, 1, 2), (1, 2, 1), (2, 5, 5), (1, 4, 8), (1, 4, 5)]
# 正确答案为:
# ans = [100, 450, 855, 1341, 1341, 811, 923, 338, 923]

# 计算结果
results = main(inp1, inp2)
for res in results:
    print(res)
